/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.gui.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.swt.widgets.Shell;
import org.toedo.controllers.DataController;
import org.toedo.gui.translations.Messages;
import org.toedo.logging.Log;
import org.toedo.model.Task;
import org.toedo.model.validators.TaskPriorityValidator;

public class ChangeTaskPriorityAction extends Action {

	private Shell mShell;
	private Task mTask;
	
	public ChangeTaskPriorityAction(Shell shell, Task task) {
		mShell = shell;
		mTask = task;
	}
	
	public void run() {
		InputDialog dialog = new InputDialog(mShell, Messages.ChangeTaskPriorityAction_Title,
				String.format(Messages.ChangeTaskPriorityAction_Message, mTask.getTitle()),
				Integer.toString(mTask.getPriority()),
				new TaskPriorityValidator());
		
		int result = dialog.open();
		String taskPriority = dialog.getValue();
		if ((result == Dialog.OK) &&
				(taskPriority != null) &&
				(!taskPriority.isEmpty())) {
			
			Log.getInstance(ChangeTaskPriorityAction.class).debug(String.format("Changing priority of task %s (%s) from %s to %s.", mTask.getTitle(), mTask.getId(), Integer.toString(mTask.getPriority()), taskPriority)); //$NON-NLS-1$
			
			DataController.getInstance().getProject().setTaskPriority(mTask, Integer.parseInt(taskPriority));
		}
	}
	
}
